<template>
  <Dialog v-model="show" @confirm="handleSubmit" title="重置密码" width="500">
    <el-form :model="formData" label-width="100px" ref="formRef" :rules="rules">
      <el-form-item label="登录密码" prop="password">
        <el-input
          type="password"
          v-model="formData.password"
          placeholder="请设置6-20位登录密码"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          type="password"
          v-model="formData.confirmPassword"
          placeholder="确认密码需要与登录密码一致"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script setup>
import { ref, watch } from "vue";
import Dialog from "@/components/dialog.vue";

const show = defineModel();
const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});
watch(show, (val) => {
  if (val) {
   
  }
});
const formData = ref({
  password: "",
  confirmPassword: "",
});

const validatePass1 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else if (value.length < 6 || value.length > 20) {
    callback(new Error("密码长度应在6-20位之间"));
  } else {
    callback();
  }
};

const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入确认密码"));
  } else if (value.length < 6 || value.length > 20) {
    callback(new Error("密码长度应在6-20位之间"));
  } else if (value !== formData.value.password) {
    callback(new Error("两次输入密码不一致"));
  } else {
    callback();
  }
};

const rules = ref({
  password: [
    { required: true, validator: validatePass1, trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: "blur" },
  ],
});
const formRef = ref();
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!");
    }
  });
};
</script>
